# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Xin Sun <sun.simpson@gmail.com>

_realname=hdf5
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_ver=1.14.2
patch=
pkgver=${_ver}${patch//-/.}
pkgrel=1
pkgdesc="General purpose library and file format for storing scientific data (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=("custom")
url="https://www.hdfgroup.org/HDF5/"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libaec"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-curl"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             $([[ ${MSYSTEM} == CLANG32 ]] || echo "${MINGW_PACKAGE_PREFIX}-fc")
             )
source=("https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-${_ver%.*}/hdf5-${_ver}/src/hdf5-${_ver}${patch}.tar.bz2"
        "hdf5-proper-library-names-mingw.patch"
        "hdf5-fix-static-lib.patch"
        "hdf5-fix-fortran-module-directory-leak.patch")
sha256sums=('ea3c5e257ef322af5e77fc1e52ead3ad6bf3bb4ac06480dd17ee3900d7a24cfb'
            'e236f5805152b25065c206922d7ec1bab05b233adac51bbd0fb1e546326162f6'
            '9d172a7f6b8f54fdbf840e032708acade4ab88c81262b45bfa85d203810962a9'
            '02b5d7335a9db628ed03b29c62d8f0e5f5618fa39de8e453aa02b530d28317a8')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${_ver}${patch}"

  apply_patch_with_msg \
    hdf5-proper-library-names-mingw.patch \
    hdf5-fix-static-lib.patch \
    hdf5-fix-fortran-module-directory-leak.patch
}

build() {
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  local -a _enable_fortran
  if [[ ${MSYSTEM} == CLANG32 ]]; then
    _enable_fortran="OFF"
  else
    _enable_fortran="ON"
  fi

  CFLAGS+=" -Wno-implicit-function-declaration" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  SZIP_INSTALL=${MINGW_PREFIX} \
    ${MINGW_PREFIX}/bin/cmake \
      -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_SHARED_LIBRARY_NAME_WITH_VERSION=ON \
      -DBUILD_TESTING=OFF \
      ${_extra_config[@]} \
      -DCMAKE_SKIP_RPATH=ON \
      -DHDF5_BUILD_HL_LIB=ON \
      -DHDF5_BUILD_CPP_LIB=ON \
      -DHDF5_BUILD_FORTRAN=${_enable_fortran} \
      -DHDF5_BUILD_TOOLS=ON \
      -DHDF5_ENABLE_DEPRECATED_SYMBOLS=ON \
      -DHDF5_ENABLE_SZIP_SUPPORT=ON \
      -DHDF5_ENABLE_Z_LIB_SUPPORT=ON \
      -DHDF5_ENABLE_ROS3_VFD=ON \
      -DHDF5_INSTALL_CMAKE_DIR="lib/cmake" \
      -DHDF5_INSTALL_DATA_DIR="share/doc/hdf5" \
      -DHDF5_INSTALL_MODULE_DIR="include" \
      -DHDF5_MSVC_NAMING_CONVENTION=OFF \
      ../${_realname}-${_ver}${patch}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}"/lib/libhdf5.settings
}
